--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--#include "data\config\item\SwingEquip.lua" once
--#include "data\config\hero\NewHeroConfig.lua" once
--开服全民竞技


--[开服活动]请求开服活动数据
function OpenServerGetActData( sysarg, packet )
	local nId = DataPack.readByte( packet )

	if not IsInOpenServerDay() then
		return
	end

	if nId >=enOpenServerId_Leveling and nId <=enOpenServerId_SoulBall then
		SendOpenServerCommonData(sysarg, nId)
	end
end


--对应活动第一项的神器奖励是否还能领取
function OpenserverActFirstAwardCanGet(sysarg, nId)
	local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, nId)
	local hadSendCount = sHadSendCountVar[1] or 0
	local subAwards = actCfg.Awards[1]
	if  not subAwards  then
		return false
	end
	if hadSendCount >= subAwards.maxCount then
		return false
	end
	return true
end
--领取开服活动奖励
function OpenServerGetActAward(sysarg, packet)
	local nId = DataPack.readByte( packet )
	local nIndex = DataPack.readByte( packet )
	local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, nId)

	if not actCfg then
		return
	end
	
	if not IsInOpenServerDay() then
		return
	end

	local nOpenServerDay = System.getDaysSinceOpenServer()
	if actCfg.openDay and nOpenServerDay < actCfg.openDay then --未到开启天数，不能领奖励	
		return
	end

	if actCfg.endDay and nOpenServerDay > actCfg.endDay then --超过开启天数，不能领奖励	
		return
	end

	local subAwards = actCfg.Awards[nIndex]
	if  not subAwards  then
		return
	end
	--[[
	if subAwards.mailReward and subAwards.mailReward == 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServer007, ttFlyTip)
		return 
	end
	]]
	local hadSendCount = sHadSendCountVar[nIndex] or 0
	if subAwards.maxCount == -1 or hadSendCount < subAwards.maxCount then --可以领
		local awardState = aAwardVar[nIndex] or OpenServerAwardState_No_Complete
		if awardState == OpenServerAwardState_Complete_No_Award then
			if not Awards.CheckBagGridCount(sysarg, subAwards.awards) then
				Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
				return
			end
			Awards.Give(sysarg,subAwards.awards,1096,Lang.LogTips.OpenServerSportsLog)
			sHadSendCountVar[nIndex] = hadSendCount + 1
			aAwardVar[nIndex] = OpenServerAwardState_Complete_Had_Award
			if subAwards.maxCount > 0 then
				OpenServerActBroadMsg(nId, Actor.getActorId(sysarg), Actor.getName(sysarg), actCfg, nIndex)
			end
			SendOpenServerCommonData(sysarg, nId)
		end
	elseif hadSendCount >= subAwards.maxCount then
		SendOpenServerCommonData(sysarg, nId)
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServer006, ttFlyTip)
		return
	end

end
--根据id获取配置或者变量
function OpenServerGetCfgAndVarById(sysarg, nId)
	local aVar = Actor.getStaticVar(sysarg)
	local sVar = System.getStaticVar()
	if not aVar.openserverActAward then
		aVar.openserverActAward = {}
	end
	if not aVar.openserverActAward[nId] then
		aVar.openserverActAward[nId] = {}
	end
	if not sVar.openserverActSendAwardCount then
		sVar.openserverActSendAwardCount = {}
	end
	if not sVar.openserverActSendAwardCount[nId] then
		sVar.openserverActSendAwardCount[nId] = {}
	end

	local actCfg = OpenServerGetCfgById(nId)
	
	return actCfg,aVar.openserverActAward[nId],sVar.openserverActSendAwardCount[nId]
end

function OpenServerGetCfgById(nId)
	local actCfg
	if nId == enOpenServerId_Leveling then
		actCfg = OpenServerCfg.Leveling
	elseif nId == enOpenServerId_Swing then
		actCfg = OpenServerCfg.Swing
	elseif nId == enOpenServerId_HeroRune then
		actCfg = OpenServerCfg.HeroRune
	elseif nId == enOpenServerId_Wuhun then
		actCfg = OpenServerCfg.Wuhun
	elseif nId == enOpenServerId_SoulBall then
		actCfg = OpenServerCfg.SoulBall
	end
	return actCfg
end


function OpenServerTriggleEvent(sysarg, nId, param1, param2) -- 当不是等级竞技的时候circle无用为0
	local var = Actor.getStaticVar(sysarg)
	if not var.osSport then
		var.osSport = {}
	end
	if nId == enOpenServerId_Leveling then
		var.osSport[nId] = param1 + param2 * OpenServerCfg.OpenServerCircleRate
	elseif nId == enOpenServerId_Swing or nId == enOpenServerId_HeroRune or nId == enOpenServerId_Wuhun or nId == enOpenServerId_SoulBall then
		var.osSport[nId] = param1
	else
		return
	end
	


	if nId == enOpenServerId_Leveling then
		OnOpenServerLevelUp(sysarg, param1, param2)
	elseif nId == enOpenServerId_Swing then
		OnOpenServerSwing(sysarg,var.osSport[nId])
	elseif nId == enOpenServerId_HeroRune then
		OnOpenServerHeroRune(sysarg,var.osSport[nId])
	elseif nId == enOpenServerId_Wuhun then
		OnOpenServerWuhun(sysarg,var.osSport[nId])
	elseif nId == enOpenServerId_SoulBall then
		OnOpenServerSoulBall(sysarg,var.osSport[nId])
	end
end
	
--六道
function OnOpenServerSoulBall(sysarg,level)
	if not IsInOpenServerDay() then
		return
	end

	local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, enOpenServerId_SoulBall)
	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay
	local nOpenServerDay = System.getDaysSinceOpenServer()

	if nOpenServerDay > actEndDay then
		return
	end

	local sendData = false
	for k,v in pairs(actCfg.Awards) do
        if level >= v.param then
			local awardState = aAwardVar[k] or OpenServerAwardState_No_Complete
			if awardState == OpenServerAwardState_No_Complete then --还未完成
				aAwardVar[k] = OpenServerAwardState_Complete_No_Award
				if nOpenServerDay >= actOpenDay then
					sendData = true
				end
			end
		end
	end


	if sendData or (setState and setState == true) then
		SendOpenServerCommonData(sysarg, enOpenServerId_SoulBall)
	end
end

--强化
function OnOpenServerWuhun(sysarg,level)
	if not IsInOpenServerDay() then
		return
	end

	local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, enOpenServerId_Wuhun)
	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay
	local nOpenServerDay = System.getDaysSinceOpenServer()

	if nOpenServerDay > actEndDay then
		return
	end

	local sendData = false
	for k,v in pairs(actCfg.Awards) do
        if level >= v.param then
			local awardState = aAwardVar[k] or OpenServerAwardState_No_Complete
			if awardState == OpenServerAwardState_No_Complete then --还未完成
				aAwardVar[k] = OpenServerAwardState_Complete_No_Award
				if nOpenServerDay >= actOpenDay then
					sendData = true
				end
			end
		end
	end
	
	if sendData or (setState and setState == true) then
		SendOpenServerCommonData(sysarg, enOpenServerId_Wuhun)
	end
	
end

function OnOpenServerHeroRune(sysarg,level)
	if not IsInOpenServerDay() then
		return
	end

	local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, enOpenServerId_HeroRune)
	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay
	local nOpenServerDay = System.getDaysSinceOpenServer()

	if nOpenServerDay > actEndDay then
		return
	end

	local sendData = false
	for k,v in pairs(actCfg.Awards) do
        if level >= v.param then
			local awardState = aAwardVar[k] or OpenServerAwardState_No_Complete
			if awardState == OpenServerAwardState_No_Complete then --还未完成
				aAwardVar[k] = OpenServerAwardState_Complete_No_Award
				if nOpenServerDay >= actOpenDay then
					sendData = true
				end
			end
		end
	end
	
	if sendData or (setState and setState == true) then
		SendOpenServerCommonData(sysarg, enOpenServerId_HeroRune)
	end
	
end


function OnOpenServerSwing(sysarg,level)
	if not IsInOpenServerDay() then
		return
	end

	local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, enOpenServerId_Swing)
	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay
	local nOpenServerDay = System.getDaysSinceOpenServer()

	if nOpenServerDay > actEndDay then
		return
	end

	local sendData = false
	for k,v in pairs(actCfg.Awards) do
        if level >= v.param then
			local awardState = aAwardVar[k] or OpenServerAwardState_No_Complete
			if awardState == OpenServerAwardState_No_Complete then --还未完成
				aAwardVar[k] = OpenServerAwardState_Complete_No_Award
				if nOpenServerDay >= actOpenDay then
					sendData = true
				end
			end
		end
	end
	
	if sendData or (setState and setState == true) then
		SendOpenServerCommonData(sysarg, enOpenServerId_Swing)
	end
	
end

--升级了
function OnOpenServerLevelUp(sysarg, nLevel, nCircle)
	if not IsInOpenServerDay() then
		return
	end
	local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, enOpenServerId_Leveling)
	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay
	local nOpenServerDay = System.getDaysSinceOpenServer()

	if nOpenServerDay > actEndDay then
		return
	end

	local sendData = false
	for k,v in pairs(actCfg.Awards) do
		if nLevel >= v.level and nCircle >= v.circle then
			local awardState = aAwardVar[k] or OpenServerAwardState_No_Complete
			if awardState == OpenServerAwardState_No_Complete then --还未完成
				aAwardVar[k] = OpenServerAwardState_Complete_No_Award
				if nOpenServerDay >= actOpenDay then
					sendData = true
				end
			end
		end
	end

	if sendData or (setState and setState == true) then
		SendOpenServerCommonData(sysarg, enOpenServerId_Leveling)
	end
end
--开服活动发邮件奖励和公告
function OpenServerActBroadMsg(nId, nActorId, sActorName, actCfg, awardsIndex)
	local msg
	local awardTable= actCfg.Awards[awardsIndex]
	if nId == enOpenServerId_Leveling then
		msg = string.format(actCfg.BroadInfo, sActorName, awardTable.circle, awardTable.level)
	elseif nId == enOpenServerId_Swing then
		msg = string.format(actCfg.BroadInfo, sActorName, SwingEquipConfig.SwingEquipTable[awardTable.param].name)
	elseif nId == enOpenServerId_HeroRune then
		local step,star = OpenServerGetFurnaceData(awardTable.param)
		msg = string.format(actCfg.BroadInfo, sActorName, step, star+1)
	elseif nId == enOpenServerId_Wuhun then
		local step,star = OpenServerGetFurnaceData(awardTable.param)
		msg = string.format(actCfg.BroadInfo, sActorName, step+1, star+1)
	elseif nId == enOpenServerId_SoulBall then
		local step,star = OpenServerGetFurnaceData(awardTable.param)	
		msg = string.format(actCfg.BroadInfo, sActorName, step+1, star+1)
	end
	System.broadcastTipmsg(msg,ttScreenCenter + ttChatWindow)
end

--[开服活动]通用界面数据(冲级，龙珠，兵魂，翅膀,坐骑)
--nId  活动id
function SendOpenServerCommonData(sysarg, nId)
	--print("nId = "..nId)
    local actCfg, aAwardVar, sHadSendCountVar = OpenServerGetCfgAndVarById(sysarg, nId)
	if not actCfg then
		return
	end
	local npack = DataPack.allocPacket(sysarg,LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sGetOpenServerAwardInfo)
	if npack == nil then 
		return 
	end
	local nOpenServerDay = System.getDaysSinceOpenServer()

	local actOpenDay = actCfg.openDay or 1
	local actEndDay = actCfg.endDay or OpenServerCfg.OpenServerDay

	DataPack.writeByte(npack, nId)
	DataPack.writeByte(npack, #actCfg.Awards)
	for k,v in pairs(actCfg.Awards) do
		DataPack.writeByte(npack, k)
		local awardState = aAwardVar[k] or OpenServerAwardState_No_Complete
		local leftCount = v.maxCount - (sHadSendCountVar[k] or 0)
		if v.maxCount == -1 then
			leftCount = -1
		end

		if  nOpenServerDay >= actOpenDay and nOpenServerDay <= actEndDay then
			--print("awardState ="..awardState)
			--print("leftCount ="..leftCount)
			DataPack.writeByte(npack, awardState)
			DataPack.writeShort(npack, leftCount)
		else
			--未到开放时间
			--print("awardState =0")
			--print("leftCount =0")
			DataPack.writeByte(npack, 0)
			DataPack.writeWord(npack, 0)
		end
	end

	local var = Actor.getStaticVar(sysarg)
	if not var.osSport then
		var.osSport = {}
	end

	--print("var.osSport[nId] or 0 ="..(var.osSport[nId] or 0))
	DataPack.writeInt(npack, var.osSport[nId] or 0)
	
	DataPack.flush(npack) 
end

--神炉等级获取阶段和星级
function OpenServerGetFurnaceData(Lv)
	return SeperateNumByMode( Lv - 1, 10 )
end


